package net.shambolica.ember.jinterface.wrappers.objects.patterns;

import com.ericsson.otp.erlang.OtpErlangObject;
import net.shambolica.ember.jinterface.wrappers.objects.EOPattern;

import java.util.Map;

/**
 * The AND predicate pattern.
 */
public class EOPAnd extends EOPattern {

    private final EOPattern left, right;

    public EOPAnd(EOPattern left, EOPattern right) {
        if (left == null || right == null) {
            throw new IllegalArgumentException("left or right hand null in OR predicate");
        }
        this.left = left;
        this.right = right;
    }

    @Override
    public Map<String, OtpErlangObject> match(OtpErlangObject term, Map<String, OtpErlangObject> acc) {
        Map<String,OtpErlangObject> leftMatch = left.match(term, mkAcc(acc));
        return leftMatch == null ? null : right.match(term, leftMatch);
    }

    @Override
    public boolean isAbstract() {
        return true;
    }

}
